local yangying = fk.CreateSkill{
  name = "xiaobai__yangying",
}

Fk:loadTranslationTable{
  ["xiaobai__yangying"] = "炴营",
  [":xiaobai__yangying"] = "结束阶段，你可以重铸任意名角色共计三张牌，若均为你的牌，你可以依次蓄谋之；"..
                          "否则失去基本牌的角色可以将一张牌当【火攻】或火【杀】对你使用，且你可以使用被重铸的牌进行响应。",
  ["#xiaobai__yangying_xumou"] = "炴营：是否蓄谋重铸的牌",
  ["#yangying__use"] = "炴营：你可以将一张牌当【火攻】或【杀】对 %dest 使用",
  ["#xiaobai__yangying_trigger"] = "炴营",
  ["#xiaobai__yangying-choose"] = "炴营：你可以重铸一名角色的一张牌",

  ["xiaobai__yangying_viewas"] = "炴营",

  ["$xiaobai__yangying1"] = "箭失左膀，方知无人可依。",
  ["$xiaobai__yangying2"] = "营门火起，不见作乱者谁。",
}

local U = require "packages.utility.utility"

yangying:addEffect("viewas", {
  pattern = "jink,nullification",
  expand_pile = function (self, player)
    return player:getTableMark("yangying")
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected > 0 then return false end
    local card = Fk:getCardById(to_select)
    if Exppattern:Parse(Fk.currentResponsePattern):match(card) then 
      return true
    end
  end,
  view_as = function (self, cards, player)
    if #cards == 0 then return nil end
    return Fk:getCardById(cards[1])
  end,
  enabled_at_response = function (self, player, response)
    return player:hasSkill(self) and player:getMark("xiaobai__yangying_viewasResopnce") > 0
  end
})

yangying:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Finish
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local chooseCards = {}
    local toplayers = {}
    for _ = 1, 3, 1 do
      if player.dead then return end
      local targets = table.filter(room:getAlivePlayers(), function(p)
        return not p:isNude() end)
      if #targets == 0 then return end
      local tos = room:askToChoosePlayers(player,{ 
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#xiaobai__yangying-choose",
        skill_name = self.name,
        cancelable = true})
      if #tos == 0 then return end
      local to = tos[1]
      if to ~= player then
        table.insertIfNeed(toplayers, to)
      end
      room:doIndicate(player, tos)
      local card = room:askToChooseCard(player,{ 
        target = to,
        flag = "he",
        skill_name = self.name})
      table.insert(chooseCards, card)
      room:recastCard({card},  to, self.name)
      room:addTableMark(player, "yangying", card)
    end
    if #toplayers == 0 then
      if room:askToSkillInvoke(player, {skill_name = self.name, prompt = "#xiaobai__yangying_xumou"}) then
        for _, cid in ipairs(chooseCards) do
          U.premeditate(player, cid, self.name)
        end
      end
    else
      room:sortByAction(toplayers)
      for _, toplayer in ipairs(toplayers) do
        local success, dat = room:askToUseActiveSkill(toplayer, {
          skill_name = "xiaobai__yangying_viewas",
          prompt = "#yangying__use::"..player.id,
          cancelable = true,
          extra_data = {
            bypass_distances = true,
            bypass_times = true,
            extraUse = true,
            exclusive_targets = {target.id},
          }
        })
        if success and dat then
          room:setPlayerMark(player, "xiaobai__yangying_viewasResopnce", 1)
          local card = Fk:cloneCard(dat.interaction)
          card.skillName = yangying.name
          card:addSubcard(dat.cards[1])
          room:useCard{
            from = toplayer,
            tos = dat.targets,
            card = card,
            extraUse = true,
          }
          room:setPlayerMark(player, "xiaobai__yangying_viewasResopnce", 0)
        end
      end
    end
    room:setPlayerMark(player, "yangying", 0)
  end
})

local yangying_viewas = fk.CreateSkill{
  name = "xiaobai__yangying_viewas",
}

yangying_viewas:addEffect("active",{
  handly_pile = true,
  interaction = function (self, player)
    return UI.CardNameBox { choices = {"fire__slash", "fire_attack"} }
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if #selected_cards == 1 then
      local card = Fk:cloneCard(self.interaction.data)
      card.skillName = "xiaobai__yangying"
      card:addSubcard(selected_cards[1])
      if player:prohibitUse(card) then return end
      if #selected == 0 then
        return to_select == Fk:currentRoom().current and
          not player:isProhibited(to_select, card) and
          card.skill:modTargetFilter(player, to_select, {}, card, {bypass_distances = true, bypass_times = true})
      else
        return card.skill:targetFilter(player, to_select, selected, {}, card, {bypass_distances = true, bypass_times = true})
      end
    end
  end,
  feasible = function (self, player, selected, selected_cards)
    if #selected_cards == 1 then
      local card = Fk:cloneCard(self.interaction.data)
      card.skillName = "xiaobai__yangying"
      card:addSubcard(selected_cards[1])
      return #selected >= card.skill:getMinTargetNum(player) and selected[1] == Fk:currentRoom().current
    end
  end,
})

local skels = {}
table.insert(skels, yangying)
table.insert(skels, yangying_viewas)

return skels